<template>
  <div id="afterPsw">
    <Header :showheaderLeft="false" :showBack="true" :isTitle="true" :showTitle="true"></Header>
    <div class="rootMain">
      <div class="con">
        <div class="inputBox inputBox2">
          <div class="name">{{$t('m.email')}}：</div>
          <div class="inputCon">
            <input type="text" v-model="email" maxlength="32" :placeholder="$t('m.emailP')" />
          </div>
        </div>
        <!-- <p>{{$t('m.pwdTips')}}</p> -->
        <div class="loginBtn loginBtn2" @click="login" v-if="!hasEmail">{{$t('m.confirm')}}</div>
        <div class="loginBtn loginBtn2" @click="login" v-else>{{$t('m.modify')}}</div>
      </div>
    </div>
  </div>
</template>
<script>
import Services from "@/service/index.js";
import { mapMutations, mapState } from "vuex";
import Header from "@/components/header";
import axios from "axios";
import { Toast } from "vant";
export default {
  data() {
    return {
      loginToken: "",
      email: "",
      hasEmail: false
    };
  },
  created() {
    this.loginToken = localStorage.getItem("token")
      ? localStorage.getItem("token")
      : "";
    this.userObj = localStorage.getItem("userObj")
      ? JSON.parse(localStorage.getItem("userObj"))
      : "";
    if (!!this.userObj.email) {
      this.hasEmail = true;
      this.email = this.userObj.email;
    }
    this.changeTitle(this.$t("m.bindEmail"));
  },
  components: { Header },
  computed: {},
  methods: {
    ...mapMutations(["changeTitle", "changetabbarIndex"]),
    login() {
      if (this.email == "") {
        Toast(this.$t("m.emailP"));
        return false;
      }
      const regex_email2 = /^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/;
      if (!regex_email2.test(this.email)) {
        Toast(this.$t("m.emailP1"));
        return false;
      }
      Services.bindEmail({
        token: this.loginToken,
        email: this.email,
        lang: localStorage.getItem("language")
      }).then(res => {
        if (res.code == "0000") {
          Toast(this.$t("m.bindSuccess"));
          this.$router.go(-1);
        } else {
          Toast(res.msg);
        }
      });
    }
  }
};
</script>
<style lang="scss" scoped type="text/scss">
#afterPsw {
  width: 100%;
  height: 100%;
  .rootMain {
    height: calc(100% - 0.88rem);
    overflow-y: auto;
    top: 0.88rem;
    // background: #434551;
    // color: #fff;
    .con {
      // background: #434551;
      height: 100%;
      padding: 0.2rem 0.5rem;
      > p {
        padding: 0 0.3rem 0.2rem;
        color: #c0c1c4;
        font-size: 0.24rem;
      }
    }
    .inputBox {
      margin: 1.1rem 0 0.15rem;
      &.inputBox2 {
        margin-top: 0;
      }
      .name {
        margin: 0.2rem 0;
        color: #40404c;
      }
      .inputCon {
        display: flex;
        justify-content: flex-start;
        align-items: center;
        width: 100%;
        height: 0.75rem;
        background: linear-gradient(to right, #f8f8f8, #d3d3d5);
        border-radius: 0.2rem;
        overflow: hidden;
        padding: 0 0.2rem;
        position: relative;
        input {
          margin: 0;
          padding: 0;
          border: none;
          outline: none;
          flex: 1;
          height: 100%;
          box-sizing: border-box;
          font-size: 0.28rem;
          color: #40404c;
          background: transparent;
          &::-webkit-input-placeholder {
            color: #999;
          }
        }
        b {
          position: absolute;
          top: 50%;
          right: 0.2rem;
          transform: translateY(-50%);

          width: 0.35rem;
          height: 0.35rem;
          &.hidePwd {
            width: 0.33rem;
            height: 0.16rem;
            background: url("~@/assets/img/common/buyan@2x.png") no-repeat
              center / 100% 100%;
          }
          &.showPwd {
            width: 0.35rem;
            height: 0.26rem;
            background: url("~@/assets/img/common/zhengyan@2x.png") no-repeat
              center / 100% 100%;
          }
        }
      }
    }
    .loginBtn {
      margin: 0 auto;
      margin-top: 2rem;
      height: 0.75rem;
      line-height: 0.75rem;
      font-size: 0.28rem;
      letter-spacing: 0.05rem;
      text-align: center;
      border-radius: 0.2rem;
      color: #fff;
      background: linear-gradient(to right, #43434e, #727181);
    }
  }
}
</style>